package ca.skygazer.shared.coords;

import java.util.Date;
import java.util.HashMap;

import ca.skygazer.shared.SgConstants;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.TimeZone;

public class LocalSiderealTime {
	
	private LatLonCoord latLon;
	private HashMap<String, Double> J2000YearData=new HashMap<String, Double>();
	private HashMap<String, Double> J2000MonthData=new HashMap<String, Double>();
	private Date date;
	
	public LocalSiderealTime(LatLonCoord latLon){
		this.latLon=latLon;
		J2000YearData.put("1998", new Double(-731.5));
		J2000YearData.put("1999", new Double(-366.5));
		J2000YearData.put("2000", new Double(-1.5));
		J2000YearData.put("2001", new Double(364.5));
		J2000YearData.put("2002", new Double(729.5));
		J2000YearData.put("2003", new Double(1094.5));
		J2000YearData.put("2004", new Double(1459.5));
		J2000YearData.put("2005", new Double(1825.5));
		J2000YearData.put("2006", new Double(2190.5));
		J2000YearData.put("2007", new Double(2555.5));
		J2000YearData.put("2008", new Double(2920.5));
		J2000YearData.put("2009", new Double(3286.5));
		J2000YearData.put("2010", new Double(3651.5));
		J2000YearData.put("2011", new Double(4016.5));
		J2000YearData.put("2012", new Double(4381.5));
		J2000YearData.put("2013", new Double(4747.5));
		J2000YearData.put("2014", new Double(5112.5));
		J2000YearData.put("2015", new Double(5477.5));
		J2000YearData.put("2016", new Double(5842.5));
		J2000YearData.put("2017", new Double(6208.5));
		J2000YearData.put("2018", new Double(6573.5));
		J2000YearData.put("2019", new Double(6938.5));
		J2000YearData.put("2020", new Double(7303.5));
		J2000YearData.put("2021", new Double(7669.5));

		J2000MonthData.put("1", new Double(0));
		J2000MonthData.put("2", new Double(31));
		J2000MonthData.put("3", new Double(59));
		J2000MonthData.put("4", new Double(90));
		J2000MonthData.put("5", new Double(120));
		J2000MonthData.put("6", new Double(151));
		J2000MonthData.put("7", new Double(181));
		J2000MonthData.put("8", new Double(212));
		J2000MonthData.put("9", new Double(243));
		J2000MonthData.put("10", new Double(273));
		J2000MonthData.put("11", new Double(304));
		J2000MonthData.put("12", new Double(334));

		J2000MonthData.put("1L", new Double(0));
		J2000MonthData.put("2L", new Double(31));
		J2000MonthData.put("3L", new Double(60));
		J2000MonthData.put("4L", new Double(91));
		J2000MonthData.put("5L", new Double(121));
		J2000MonthData.put("6L", new Double(152));
		J2000MonthData.put("7L", new Double(182));
		J2000MonthData.put("8L", new Double(213));
		J2000MonthData.put("9L", new Double(244));
		J2000MonthData.put("10L", new Double(274));
		J2000MonthData.put("11L", new Double(305));
		J2000MonthData.put("12L", new Double(335));
	}
	
	public double getLst(){
		date = new Date();
		String utc = DateTimeFormat.getFormat("yyyy,M,d,HH,mm,ss").format(date,TimeZone.createTimeZone(0));
		String[] splitUtc=utc.split(",");		
		
	    int year=Integer.parseInt(splitUtc[0]);
		int month=Integer.parseInt(splitUtc[1]);
		int day=Integer.parseInt(splitUtc[2]);
		double UT=Double.parseDouble(splitUtc[3])+
			Double.parseDouble(splitUtc[4])/60+
			Double.parseDouble(splitUtc[5])/3600;	
		String yearString = String.valueOf(year);
		String monthString = String.valueOf(month);

		if(isLeapYear(year)) monthString+="L";
		double d=J2000YearData.get(yearString).doubleValue()+
			J2000MonthData.get(monthString)+day+UT/24.0;
		 return (100.46+0.985647*d+latLon.lon*SgConstants.RAD2DEG+SgConstants.HRS2DEG*UT)%360*SgConstants.DEG2RAD;
	}
	
	private boolean isLeapYear(int year){
		if(year%400==0) return true;
		if((year%4==0) && (year%100!=0)) return true;
		return false;
	}
	
}
